<!-- views/purchase/components/SupplierFilter.vue -->
<template>
  <el-card class="filter-card" shadow="never">
    <el-form :model="queryParams" inline>
      <el-form-item label="供应商名称">
        <el-input
          v-model="queryParams.name"
          placeholder="输入供应商名称"
          clearable
          style="width: 200px"
        />
      </el-form-item>
      
      <el-form-item label="联系人">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="输入联系人"
          clearable
          style="width: 150px"
        />
      </el-form-item>
      
      <el-form-item label="联系电话">
        <el-input
          v-model="queryParams.phone"
          placeholder="输入联系电话"
          clearable
          style="width: 150px"
        />
      </el-form-item>
      
      <el-form-item label="邮箱">
        <el-input
          v-model="queryParams.email"
          placeholder="输入邮箱"
          clearable
          style="width: 150px"
        />
      </el-form-item>
      
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" clearable placeholder="全部状态" class="form-item-width">
          <el-option label="合作中" value="active" />
          <el-option label="暂停合作" value="inactive" />
          <el-option label="黑名单" value="blacklisted" />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleSearch">
          查询
        </el-button>
        <el-button :icon="Refresh" @click="handleReset">
          重置
        </el-button>
        <el-button 
          link
          :icon="SetUp" 
          @click="toggleAdvancedFilter"
        >
          高级筛选
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 高级筛选 -->
    <el-collapse-transition>
      <div v-show="showAdvancedFilter" class="advanced-filter">
        <el-divider content-position="left">高级筛选</el-divider>
        <el-form :model="queryParams" inline>
          <el-form-item label="地区">
            <el-cascader
              v-model="queryParams.region"
              :options="regionOptions"
              placeholder="选择地区"
              style="width: 200px"
            />
          </el-form-item>
          
          <el-form-item label="合作时间">
            <el-date-picker
              v-model="queryParams.cooperationDateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          
          <el-form-item label="评分">
            <el-rate
              :model-value="Number(queryParams.minRating)"
              :max="5"
              allow-half
              disabled
            />
            <span class="rating-text">以上</span>
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>
  </el-card>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { Search, Refresh, SetUp } from '@element-plus/icons-vue'

const showAdvancedFilter = ref(false)

// 地区选项数据
const regionOptions = [
  {
    value: 'zhejiang',
    label: '浙江省',
    children: [
      {
        value: 'hangzhou',
        label: '杭州市',
        children: [
          { value: 'xihu', label: '西湖区' },
          { value: 'binjiang', label: '滨江区' }
        ]
      }
    ]
  }
]

// 定义查询参数接口
interface QueryParams {
  name: string
  contactPerson: string
  phone: string
  email: string
  status: string
  region: string[]
  cooperationDateRange: string[]
  minRating: number
  page: number
  limit: number
}

const props = defineProps<{
  queryParams: QueryParams
}>()

const emit = defineEmits<{
  (e: 'search'): void
  (e: 'reset'): void
}>()

const handleSearch = () => {
  emit('search')
}

// 监听region变化并触发搜索
watch(() => props.queryParams.region, () => {
  // 当region变化时触发搜索
  emit('search')
}, { deep: true })

const handleReset = () => {
  emit('reset')
}

const toggleAdvancedFilter = () => {
  showAdvancedFilter.value = !showAdvancedFilter.value
  console.log('高级筛选状态:', showAdvancedFilter.value)
}
</script>

<style scoped>
.filter-card {
  margin-bottom: 20px;
}

.advanced-filter {
  margin-top: 16px;
}

.advanced-filter :deep(.el-divider) {
  margin: 16px 0;
}

.rating-text {
  margin-left: 8px;
  color: var(--el-text-color-secondary);
}
</style>